<template>
    <div>
        <ListPage>
            <template #filter>
                <el-form ref="form" :model="searchForm" inline label-width="80px">
                    <el-form-item label="">
                        <el-input v-model="searchForm.name" placeholder="供货商名称" clearable style="width: 200px;"></el-input>
                    </el-form-item>
                    <el-form-item label="">
                        <el-input v-model="searchForm.address" placeholder="地址" clearable style="width: 200px;"></el-input>
                    </el-form-item>
                    <el-form-item label="">
                        <el-input v-model="searchForm.info" placeholder="描述" clearable style="width: 200px;"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" size="small" @click="submitSearchForm">搜索</el-button>
                        <el-button size="small">重置</el-button>
                    </el-form-item>
                </el-form>
            </template>

            <template #operate>
                <div>
                    <el-button
                    type="success"
                    size="small"
                    @click="newVisable = true"
                >
                        创建供货商
                    </el-button>
                </div>
            </template>
                
            <template #table>
                <el-table :data="tableData" v-loading="loading" style="width: 100%" size="medium">
                    <el-table-column type="index" width="60" label="序号">
                    </el-table-column>
                    <el-table-column
                        prop="name"
                        :show-overflow-tooltip="true"
                        label="名称"
                    >
                    </el-table-column>
                    <el-table-column
                        prop="address"
                        :show-overflow-tooltip="true"
                        label="地址"
                    >
                    </el-table-column>
                    <el-table-column
                        prop="tel"
                        :show-overflow-tooltip="true"
                        label="联系电话"
                    >
                    </el-table-column>
                    <el-table-column
                        prop="info"
                        :show-overflow-tooltip="true"
                        label="备注"
                    >
                    </el-table-column>
                    <el-table-column width="150" fixed="right" label="操作">
                        <template v-slot="scope">
                            <el-button
                                type="warning"
                                size="small"
                                @click="editBtn(scope.row.cn)"
                            >
                                修改</el-button
                            >
                            <el-button
                                type="danger"
                                size="small"
                                @click="delBtn(scope.row.cn)"
                            >
                                删除</el-button
                            >
                        </template>
                    </el-table-column>
                </el-table>
            </template>

            <template #pagination>
                <div>
                    <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page.sync="searchForm.currentPage"
                    :page-sizes="[20, 50, 100]"
                    :page-size="searchForm.pageSize"
                    layout="total,sizes, prev, pager, next,jumper"
                    :total="searchForm.total"
                    >
                    </el-pagination>
                </div>
            </template>
        </ListPage>

        <!--创建弹出框-->
        <el-dialog
            title="创建供货商信息"
            :visible.sync="newVisable"
            width="20%"
        >
            <el-form
                :model="newForm"
                :rules="rules"
                ref="newForm"
                label-width="100px"
                class="demo-ruleForm"
            >
                <el-form-item label="名称：" prop="name">
                    <el-input
                        v-model="newForm.name"
                        placeholder="供应商名称"
                    ></el-input>
                </el-form-item>
                <el-form-item label="地址：" prop="address">
                    <el-input
                        type="text"
                        v-model="newForm.address"
                        placeholder="供应商地址"
                    ></el-input>
                </el-form-item>
                <el-form-item label="电话：" prop="tel">
                    <el-input
                        type="text"
                        v-model="newForm.tel"
                        placeholder="联系电话"
                    ></el-input>
                </el-form-item>
                <el-form-item label="描述：">
                    <el-input
                        type="textarea"
                        v-model="newForm.info"
                        placeholder="供应商的简单描述"
                    ></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button
                        type="primary"
                        size="small"
                        @click="submitNewForm('newForm')"
                    >
                        提交</el-button
                    >
                    <el-button
                        size="small"
                        @click="
                            () => {
                                this.$refs['newForm'].resetFields();
                                newForm = {};
                                newVisable = false;
                            }
                        "
                        >关闭</el-button
                    >
                </el-form-item>
            </el-form>
        </el-dialog>
        <!--修改弹出框-->
        <el-dialog
            title="修改供货商信息"
            :visible.sync="editVisable"
            width="20%"
        >
            <el-form
                :model="editForm"
                :rules="rules"
                ref="editForm"
                label-width="100px"
                class="demo-ruleForm"
            >
                <el-form-item label="名称：" prop="name">
                    <el-input
                        v-model="editForm.name"
                        placeholder="供应商名称"
                    ></el-input>
                </el-form-item>
                <el-form-item label="地址：" prop="address">
                    <el-input
                        type="text"
                        v-model="editForm.address"
                        placeholder="供应商地址"
                    ></el-input>
                </el-form-item>
                <el-form-item label="电话：" prop="tel">
                    <el-input
                        type="text"
                        v-model="editForm.tel"
                        placeholder="联系电话"
                    ></el-input>
                </el-form-item>
                <el-form-item label="描述：">
                    <el-input
                        type="textarea"
                        v-model="editForm.info"
                        placeholder="供应商的简单描述"
                    ></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button
                        type="primary"
                        size="small"
                        @click="submitEditForm('editForm')"
                    >
                        提交</el-button
                    >
                    <el-button
                        size="small"
                        @click="
                            () => {
                                this.$refs['editForm'].resetFields();
                                editForm = {};
                                editVisable = false;
                            }
                        "
                        >关闭</el-button
                    >
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>
<script>
import { popup } from "@/assets/js/common";
import {
    delSupplier,
    queryByCn,
    queryPageSupplier,
    saveSupplier,
    updateSupplier,
} from "@/api/inventory_management/supplier/supplierApi";

export default {
    data() {
        return {
            loading: false,
            tableData: [],
            editVisable: false,
            editForm: {},
            searchForm: {
                pageSize: 20,
            },
            newVisable: false,
            newForm: {},
            rules: {
                name: [
                    {
                        required: true,
                        message: "供货商名称不能为空",
                        trigger: "blur",
                    },
                ],
                address: [
                    {
                        required: true,
                        message: "供货商地址不能为空",
                        trigger: "blur",
                    },
                ],
                tel: [
                    {
                        required: true,
                        message: "供货商联系电话不能为空",
                        trigger: "blur",
                    },
                ],
            },
        };
    },
    methods: {
        subSearchForm() {
            this.init();
        },
        init() {
            this.loading = true;
            queryPageSupplier(this.searchForm).then((res) => {
                res = res.data;
                if (res.code == 200) {
                    this.tableData = res.data.records;
                    this.searchForm.total = res.data.total;
                    this.searchForm.pageSize = res.data.size;
                    this.searchForm.currentPage = res.data.current;
                } else {
                    popup(res.msg, "error");
                }
            })
            .finally(() => {
                this.loading = false;
            });
        },
        /*分页*/
        handleSizeChange(val) {
            this.searchForm.pageSize = val;
            this.init();
            console.log(`每页 ${val} 条`);
        },
        handleCurrentChange(val) {
            this.searchForm.currentPage = val;
            this.init();
            console.log(`当前页: ${val}`);
        },
        submitSearchForm() {
            this.init();
        },
        submitNewForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    saveSupplier(this.newForm).then((res) => {
                        res = res.data;
                        if (res.code == 200) {
                            popup("添加成功");
                            this.newVisable = false;
                            this.newForm = {};
                            this.init();
                        } else {
                            popup(res.msg, "error");
                        }
                    });
                }
            });
        },
        delBtn(cn) {
            this.$confirm("确定要删除这条记录" + "？", "警示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    delSupplier({ cn: cn }).then((res) => {
                        res = res.data;
                        if (res.code == 200) {
                            popup("删除成功");
                            this.init();
                        } else {
                            popup(res.msg, "warning");
                        }
                    });
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消操作",
                    });
                });
        },
        editBtn(cn) {
            queryByCn({ cn: cn }).then((res) => {
                res = res.data;
                if (res.code == 200) {
                    this.editForm = { ...res.data };
                    this.editVisable = true;
                } else {
                    popup(res.msg, "error");
                }
            });
        },
        submitEditForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    updateSupplier(this.editForm).then((res) => {
                        res = res.data;
                        if (res.code == 200) {
                            popup("修改成功");
                            this.editVisable = false;
                            this.editForm = {};
                            this.init();
                        } else {
                            popup(res.msg, "error");
                        }
                    });
                }
            });
        },
    },
    mounted() {
        this.init();
    },
};
</script>
<style scoped>
</style>